package Mybatis.Utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @author wq
 * @since 2022-06-22 14:50
 */

public class SessionUtils {

    public static SqlSession getSqlSession(){
        //读取读取mybatis-config.xml
        InputStream resourceAsStream = Resources.class.getResourceAsStream("/mybatis-config.xml");
        //第一步获取SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //builder的build方法获取SqlSessionFactory对象
        SqlSessionFactory build = builder.build(resourceAsStream);
        SqlSession sqlSession = build.openSession(true);
        return sqlSession;
    }
}
